package com.jubao.ejb.business.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;

import org.apache.log4j.Logger;
import org.jboss.ejb3.annotation.LocalBinding;
import org.jboss.ejb3.annotation.RemoteBinding;

import com.jubao.ejb.business.Business;
import com.jubao.ejb.business.WebsiteBusinessLocal;
import com.jubao.ejb.business.WebsiteBusinessRemote;
import com.jubao.ejb.business.util.BusinessConstant;
import com.jubao.ejb.entity.Category;
import com.jubao.ejb.entity.GeneRelation;
import com.jubao.ejb.entity.Website;

@Stateless
@LocalBinding(jndiBinding = BusinessConstant.WebsiteBusinessLocalJndiName)
@RemoteBinding(jndiBinding = BusinessConstant.WebsiteBusinessRemoteJndiName)
public class WebsiteBusinessImpl extends Business implements WebsiteBusinessLocal, WebsiteBusinessRemote {
	static Logger logger = Logger.getLogger(WebsiteBusinessImpl.class);

	public List<Website> getWebsiteList(long areaCategorySysId, long websiteCategorySysId, int pgno, int pgsize) {
		int relationId1 = areaCategorySysId > -1 ? BusinessConstant.RelationID.WEBSITE_IS_MEMBER_OF_CATEGORY.getId()
				: -1;
		int relationId2 = websiteCategorySysId > -1 ? BusinessConstant.RelationID.WEBSITE_IS_MEMBER_OF_CATEGORY.getId()
				: -1;

		List websites = super.geneRelationDao.getEntityList(Website.class.getName(), relationId1, areaCategorySysId,
				relationId2, websiteCategorySysId, pgno, pgsize);
		return websites;
	}

	public long getWebsiteCount(long areaCategorySysId, long websiteCategorySysId) {
		int relationId1 = areaCategorySysId > -1 ? BusinessConstant.RelationID.WEBSITE_IS_MEMBER_OF_CATEGORY.getId()
				: -1;
		int relationId2 = websiteCategorySysId > -1 ? BusinessConstant.RelationID.WEBSITE_IS_MEMBER_OF_CATEGORY.getId()
				: -1;

		Long result = super.geneRelationDao.getEntityCount(Website.class.getName(), relationId1, areaCategorySysId,
				relationId2, websiteCategorySysId);
		return result;
	}

	public HashMap<String, Object> getRelatedWebsiteInfo(String url, int maxRelatedSiteNum) {
		HashMap<String, Object> result = new HashMap<String, Object>();
		Website website = websiteDao.find(url);
		if (website != null) {

			//content related
			List mergeWebsites = new ArrayList();
			mergeWebsites.add(website);
			Category pcat = website.getPrimaryWebsiteCategory();
			if (pcat != null) {
				List<Category> pcattrail = categoryDao.getPrimaryCategoryTrail(pcat.getSysId());
				result.put("primarypath", pcattrail);

				//get related content websites
				if (pcattrail != null) {
					for (int i = 0; i < pcattrail.size(); i++) {
						Category cat = pcattrail.get(i);
						List websites = geneRelationDao.getEntityList(Website.class.getName(),
								BusinessConstant.RelationID.WEBSITE_IS_MEMBER_OF_CATEGORY.getId(), cat.getSysId(), 0,
								maxRelatedSiteNum);
						mergeWebsites.addAll(websites);
						if (mergeWebsites.size() > maxRelatedSiteNum)
							break;
					}
				}
			}

			//area related
			List mergeAreaWebsites = new ArrayList();
			Category pareacat = website.getPrimaryAreaCategory();
			if (pareacat != null) {
				List<Category> pcattrail = categoryDao.getPrimaryCategoryTrail(pcat.getSysId());
				result.put("primaryareapath", pcattrail);

				//get related area websites
				if (pcattrail != null) {
					for (int i = 0; i < pcattrail.size(); i++) {
						Category cat = pcattrail.get(i);
						List websites = geneRelationDao.getEntityList(Website.class.getName(),
								BusinessConstant.RelationID.WEBSITE_IS_MEMBER_OF_CATEGORY.getId(), cat.getSysId(), 0,
								maxRelatedSiteNum);
						mergeAreaWebsites.addAll(websites);
						if (mergeAreaWebsites.size() > maxRelatedSiteNum)
							break;
					}
				}
			}
			
			mergeWebsites.addAll(mergeAreaWebsites);
			result.put("site", mergeWebsites);

			List s2cList = new ArrayList();
			for (int i = 0; i < mergeWebsites.size(); i++) {
				Website ws = (Website) mergeWebsites.get(i);
				List<GeneRelation> grs = geneRelationDao.getRelationList(ws.getSysId(),
						BusinessConstant.RelationID.WEBSITE_IS_MEMBER_OF_CATEGORY.getId(), -1, 0, 100);
				s2cList.addAll(grs);
			}
			result.put("s2c", s2cList);
		}
		return result;
	}

	@TransactionAttribute(TransactionAttributeType.REQUIRED)
	public Website registerWebsite(String url) {
		Website website = websiteDao.find(url);
		if (website == null) {
			website = new Website();
			website.setId(url);
			website.setName(url);
			website.setFullName("Unknow Website Full Name");
			website.setRank(-1);
			website.setStatus(BusinessConstant.Status.IS_APPROVING.getId());
			website =websiteDao.save(website);
		}
		return website;
	}

	public void add2CateogryTrail(long websiteSysId, long categorySysId) {
		Website website = websiteDao.find(websiteSysId);
		List<Category> primaryCategoryTrail = categoryDao.getPrimaryCategoryTrail(categorySysId);
		if (website != null && primaryCategoryTrail != null && primaryCategoryTrail.size() > 0) {
			for (int i = 0; i < primaryCategoryTrail.size(); i++) {
				Category category = primaryCategoryTrail.get(i);
				GeneRelation gr = geneRelationDao.find(websiteSysId,
						BusinessConstant.RelationID.WEBSITE_IS_MEMBER_OF_CATEGORY.getId(), category.getSysId());
				if (gr == null) {
					gr = new GeneRelation();
					gr.setEntity(website);
					gr.setRelationId(BusinessConstant.RelationID.WEBSITE_IS_MEMBER_OF_CATEGORY.getId());
					gr.setEntity2(category);
					geneRelationDao.save(gr);
				}
			}
		}
	}

	public void removeFromCategoryTrail(long websiteSysId, long categorySysId) {
		Website website = websiteDao.find(websiteSysId);
		List<Category> primaryCategoryTrail = categoryDao.getPrimaryCategoryTrail(categorySysId);
		if (website != null && primaryCategoryTrail != null && primaryCategoryTrail.size() > 0) {
			for (int i = 0; i < primaryCategoryTrail.size(); i++) {
				Category category = primaryCategoryTrail.get(i);
				GeneRelation gr = geneRelationDao.find(websiteSysId,
						BusinessConstant.RelationID.WEBSITE_IS_MEMBER_OF_CATEGORY.getId(), category.getSysId());
				if (gr != null) {
					geneRelationDao.remove(gr);
				}
			}
		}
	}

	/* (non-Javadoc)
	 * @see com.jubao.ejb.business.WebsiteBusiness#getNewRegisteredWebsiteCount()
	 */
	public long getNewRegisteredWebsiteCount() {
		return websiteDao.getWebsiteCount(BusinessConstant.Status.IS_APPROVING.getId());
	}

	/* (non-Javadoc)
	 * @see com.jubao.ejb.business.WebsiteBusiness#getNewRegisteredWebsiteList(int, int)
	 */
	public List<Website> getNewRegisteredWebsiteList(int pgno, int pgsize) {
		return websiteDao.getWebsiteList(BusinessConstant.Status.IS_APPROVING.getId(),pgno,pgsize);
	}

	public Website saveWebSiteProfile(Website website) {
		Website result = null;
		if(website!=null){
			if(website.getPrimaryAreaCategory()==null || website.getPrimaryWebsiteCategory()==null)
			website.setStatus(BusinessConstant.Status.IS_APPROVING.getId());
			result = websiteDao.save(website);
		}
		return result;
	}
}
